<?php


namespace api\zhengmaotong\controller;



use api\zhengmaotong\model\BannerModel;
use api\zhengmaotong\model\UserModel;
use api\zhengmaotong\validate\UserValidate;
use cmf\controller\RestBaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\Filesystem;
use think\Request;

class UserController extends RestBaseController
{
    //注册
    public function register()
    {
        // halt(cmf_password(123123));
        $data = input('post.');
        $validate = new UserValidate();
        $res = $validate->scene('register')->check($data);
        if (!$res){
            $this->error($validate->getError());
        }
        $user = UserModel::where('mobile',$data['mobile'])->find();
        if ($user){
            $this-> error('该用户已被注册');
        }
        $c = checkCode($data['mobile'],$data['code']);
        if (!$c){
            $this->error('验证码错误');
        }
        $data['create_time'] = time();
        if (UserModel::create($data)){
            $this->success('注册成功');
        }


    }
    //登录
    public function login()
    {
        $data = input('post.');
        $validate = new UserValidate();
        $res = $validate->scene('register')->check($data);
        if (!$res){
            $this->error($validate->getError());
        }
        $user = \api\zhengmaotong\model\UserModel::where('mobile',$data['mobile'])->find();
        if (!$user){
            $this-> error('该用户未注册');
        }
        $token= signToken($user['id'],$user['mobile']);
        $this->success('登录成功',[
            'data' => $token
        ]);
        $c = checkCode($data['mobile'],$data['code']);
        if ($c){
            $token= signToken($user['id'],$user['mobile']);
            $this->success('登录成功',[
                'data' => $token
            ]);
        }else{
            $this->error('登陆失败');
        }
        $this->success('登录成功');
//        //返回token
//        return success('登陆成功',[
//            //JWT
//            'data' => signToken($user['id'],$user['mobile']),
//        ]);
    }
    //发送验证码
    public function SmsCode()
    {
        $mobile = input('post.mobile');

         return SendCode($mobile);
    }
    //上传文件
    public function UploadImage(Request $request)
    {
        $image = $request->file('image');
        $savename = \think\facade\Filesystem::disk('public')->putFile( 'user', $image);
        if ($savename){
            $this->success('上传成功',[
                'data' => $savename
            ]);
        }else{
            $this->error('上传失败');
        }
    }
    //查询用户数据
    public function userList()
    {
        $id = input('id/d');
        if (!$id) {
            $this->error('参数错误');
        }
        $data = UserModel::field('id,image,user_name,mobile')->find($id);
        if (!$data){
            $this->error('无用户');
        }
        $this->success('查询成功',[
            'data' => $data
        ]);
    }
    //上传图片
    public function upload()
    {
        $mobile = input('mobile');
        // 获取表单上传文件 例如上传了001.jpg
        $image = request()->file('image');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile('topic', $image);
        if (!$mobile) {
            return error('请输入手机号');
        }
        $image1 = request()->domain() . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . $savename;
        $upload = UserModel::where('mobile', '=', $mobile)->find();
        if (!$upload) {
            $this->error('未查询到数据');
        }
        $upload->image = $image1;

        if ($upload->save()) {
            $this->success('上传成功', [
                'data' => $image1
            ]);
        } else {
            $this->error('上传失败');
        }
    }
    //获取用户信息
    public function getuserInfo(){
        $header = request()->header();

        //isset检查是否设置
        if(!isset($header['token'])||empty($header['token'])){
            $this->error('token错误');
        }
        $key= new key(config('app.jwt_key'),'HS256');
        try{
            $res = JWT::decode($header['token'],$key);
        }catch (\Exception $exception){
            $this->error($exception->getMessage());
        }
        $userInfo=UserModel::field('id,image,mobile')
            ->where('id',$res->data->user_id)
            ->find();
        $userInfo['image']= $this->request->domain().'/stroage/'.$userInfo['image'];
        $this->success('用户信息',$userInfo);
    }
    //修改用户信息
    public function updateUserinfo(){
        $header = request()->header();
        //isset检查是否设置
        if(!isset($header['token'])||empty($header['token'])){
            $this->error('token错误');
        }
        $key= new Key(config('app.jwt_key'),'HS256');
        try{
            $res = JWT::decode($header['token'],$key);
        }catch (\Exception $exception){
            $this->error($exception->getMessage());
        }

        $data = input('post.');
        //验证
//        UserModel::where('id',$res->user->data->user_id)->updata([
//            'image'=>$data['image']
//        ]);
//        $vaildate = new UserValidate();
//        $res = $vaildate->scene('edit')->check($data);
//        if (!$res) {
//            $this->error($vaildate->getError());
//        }
        $user = UserModel::find($res->data->user_id);
        $user->mobile = $data['mobile'];
        $user->name = $data['name'];
        $user->image = $data['image'];
        $user->update_time = time();
        if($user->save()){
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }
    public function edit()
    {
        $data = input('post.');
        //halt($data);
        if (!$data){
            $this->error('参数错误');
        }
        $validate = new UserValidate();
        $res = $validate->scene('edit')->check($data);
        if (!$res){
            $this->error($validate->getError());
        }
        if (!$data['id']){
            $this->error('ID错误');
        }
        $user = UserModel::find($data['id']);
        $user->image = $data['image'];
        $user->user_name = $data['user_name'];
        $user->mobile = $data['mobile'];
        $user->update_time = time();
        if ($user->save()){
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }
    }
}